<template>
    <div class="login-wrap">
        <el-button :plain="true" @click="open">{{message}}</el-button>
        <div class="ms-login">
            <div class="ms-title">Echo-Media</div>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" placeholder="username">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="password" v-model="ruleForm.password"
                              @keyup.enter.native="submitForm('ruleForm')">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                </div>
                <el-row type="flex" justify="start">
                    <auth ref="auth" v-on:loginEvent="login" v-on:logoutEvent="logout"></auth>
                </el-row>
                <p class="login-tips">Tips : chen/123123 </p>
            </el-form>
        </div>
    </div>
</template>

<script>
    import GithubAuth from '../auth/GithubAuth';
    import ElRow from "element-ui/packages/row/src/row";

    export default {
        components: {
            ElRow,
            'auth': GithubAuth
        },
        data: function () {
            return {
                user: null,
                ruleForm: {
                    username: '',
                    password: '',
                    avatar: ''
                },
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'}
                    ]
                },
                message: ""
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios.post("/echo-auth-system/user/login", this.$qs.stringify({
                            username: this.ruleForm.username,
                            password: this.ruleForm.password
                        })).then(res => {
                            console.log(res);
                            let result = res.data;
                            if (result && result.code === 200) {
                                //持久化 accessToken、refreshToken 到cookie中
                                let avatar = result.data.avatar;
                                let username = result.data.username;
                                let access_token = result.data.access_token;
                                let refresh_token = result.data.refresh_token;
                                this.$cookie.set('echo_access_token', access_token, 30);
                                this.$cookie.set('echo_refresh_token', refresh_token, 30);
                                this.$cookie.set('loginUser', username);
                                this.$cookie.set('avatar', avatar);
                                //回调后登录首页
                                this.$router.push('/dashboard');
                            } else {
                                this.open(result.data.message, 'warning');
                                return false;
                            }
                        });
                    } else {
                        this.open("请填写用户名和密码", 'error');
                        return false;
                    }
                });
            },
            open(responseMsg, messageType) {
                this.$message({
                    type: messageType,
                    showClose: true,
                    message: responseMsg
                });
            },
            openHtml(html) {
                this.$message({
                    dangerouslyUseHTMLString: true,
                    message: html
                });
            },
            login: function (user) {
                this.user = user;
                if (user !== undefined) {
                    //入库gitHub提供的用户名和密码
                    this.$axios.post("/echo-auth-system/oauth/store", this.$qs.stringify(user)).then(res => {
                        let result = res.data;
                        if (result) {
                            if (result.code === 200) {
                                //持久化 accessToken、refreshToken 到cookie中
                                let access_token = result.data.access_token;
                                let refresh_token = result.data.refresh_token;
                                this.$cookie.set('echo_access_token', access_token, 30);
                                this.$cookie.set('echo_refresh_token', refresh_token, 30);
                                this.$cookie.set('loginUser', user.login);
                                this.$cookie.set('avatar', this.user.avatar);
                                this.$router.push('/dashboard');
                            }
                        }

                    });

                }
            },
            logout: function () {
                this.$axios.post("/echo-auth-system/oauth/loginOut", this.$qs.stringify({
                    access_token: this.$cookie.get("echo_access_token")
                })).then(res => {
                    let result = res.data;
                    if (result.code === 200) {
                        console.log("登录用户已移除")
                    }
                });
                this.user = null;
                this.$cookie.delete('accessToken');
                this.$cookie.delete("echo_access_token");
                this.$cookie.delete("echo_refresh_token");
                this.$cookie.delete("loginUser");
                this.$cookie.delete("avatar");
            }
        }
    }
</script>

<style scoped>
    .login-wrap {
        position: relative;
        width: 100%;
        height: 100%;
        background-image: url(../../assets/img/login-bg.jpg);
        background-size: 100%;
    }

    .ms-title {
        width: 100%;
        line-height: 50px;
        text-align: center;
        font-size: 20px;
        color: #fff;
        border-bottom: 1px solid #ddd;
    }

    .ms-login {
        position: absolute;
        left: 50%;
        top: 50%;
        width: 350px;
        margin: -190px 0 0 -175px;
        border-radius: 5px;
        background: rgba(255, 255, 255, 0.3);
        overflow: hidden;
    }

    .ms-content {
        padding: 30px 30px;
    }

    .login-btn {
        text-align: center;
    }

    .login-btn button {
        width: 100%;
        height: 36px;
        margin-bottom: 10px;
    }

    .login-tips {
        font-size: 12px;
        line-height: 30px;
        color: #fff;
    }
</style>